﻿Public Partial Class CustomerMaster
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "Customer Master"
            Session("HideMENU") = "M"

            ViewState("gridCustomer") = clsGet.Get_CustomerMaster("")
            gridCustomer.DataSource = ViewState("gridCustomer")
            gridCustomer.DataBind()

            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnUpdate.Visible = False
            End If
        End If
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        ' Dim Connector As New SAP.Connector.Destination
        Dim ClassSAP As New Class_SAP
        Dim SAP_Data As Object = ClassSAP.SAP_OPEN()
        ' SAP_Data = New SAP_IPORT.SAP_IPORT_FUNCTION


        'Connector.AppServerHost = "10.16.19.40"    '"hq-bwprd"
        'Connector.SystemNumber = 10
        'Connector.Client = 900
        'Connector.Username = "SDRFC05"
        'Connector.Password = "uio37dfg"

        ''Connector.Username = "sdpanasasa"
        ''Connector.Password = "papapa04"

        'SAP_Data.Connection = New SAP.Connector.SAPConnection(Connector)
        'SAP_Data.Connection.Open()

        Dim ZBABI_INPUT As New SAP_IPORT.ZBAPI_CUST_INPUTTable
        Dim ZBABI_SOLDTO As New SAP_IPORT.ZBAPI_SOLD_TOTable
        Dim ZBABI_BILLTO As New SAP_IPORT.ZBAPI_BILL_TOTable
        Dim EX_SUBRC As Integer

        Try
            SAP_Data.Zbapi_Iport_Customer_Master("", "", EX_SUBRC, ZBABI_BILLTO, ZBABI_INPUT, ZBABI_SOLDTO)
            ' SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK, ZBABI_HD, "IR0003", "", ZBABI_VT, Ex_Belnr, Ex_Bukrs, Ex_Gjahr, CustomerTable, BABITable)
            If (ZBABI_BILLTO.Count > 0) Or (ZBABI_SOLDTO.Count > 0) Then
                ' Response.Write("<SCRIPT>alert('" & ZBABI_BILLTO.Item(0).Sold_To.ToString() & "');</SCRIPT>")


                '***********************************************************************
                '***********  INSERT ข้อมูล CUSTOMER *************************************
                '***********************************************************************
                Dim conn As New System.Data.SqlClient.SqlConnection(connString)
                Dim clsDB As New ClassDatabase
                Dim clsFc As New ClassFunction
                Dim condition As Boolean = True
                Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
                Application.Lock()
                conn.Open()
                ' Dim ID_Customer As String = clsDB.GeneratorID(conn, "Customer")
                ' Dim ID_BillTo As String = clsDB.GeneratorID(conn, "Customer_Billto")
                tran = clsDB.BeginTran(conn)
                For i As Integer = 0 To ZBABI_SOLDTO.Count - 1
                    If (clsFc.UpdateCustomer(ZBABI_SOLDTO.Item(i).Sold_To, ZBABI_SOLDTO.Item(i).Name_1, ZBABI_SOLDTO.Item(i).Name_2, ZBABI_SOLDTO.Item(i).Name_3, ZBABI_SOLDTO.Item(i).Street, ZBABI_SOLDTO.Item(i).Str_Suppl3, ZBABI_SOLDTO.Item(i).Location, ZBABI_SOLDTO.Item(i).District, ZBABI_SOLDTO.Item(i).City, ZBABI_SOLDTO.Item(i).Postl_Cod1, ZBABI_SOLDTO.Item(i).Country, "0", conn, tran) = 0) Then
                        If (clsFc.InsertCustomer(ZBABI_SOLDTO.Item(i).Sold_To, ZBABI_SOLDTO.Item(i).Name_1, ZBABI_SOLDTO.Item(i).Name_2, ZBABI_SOLDTO.Item(i).Name_3, ZBABI_SOLDTO.Item(i).Street, ZBABI_SOLDTO.Item(i).Str_Suppl3, ZBABI_SOLDTO.Item(i).Location, ZBABI_SOLDTO.Item(i).District, ZBABI_SOLDTO.Item(i).City, ZBABI_SOLDTO.Item(i).Postl_Cod1, ZBABI_SOLDTO.Item(i).Country, "0", conn, tran) < 1) Then
                            condition = False
                        End If
                    End If
                Next
                If (clsFc.UpdateCustomer("9999999999", "IRPC", "", "", "", "", "", "", "", "", "TH", "0", conn, tran) = 0) Then
                    If (clsFc.InsertCustomer("9999999999", "IRPC", "", "", "", "", "", "", "", "", "TH", "0", conn, tran) < 1) Then
                        condition = False
                    End If
                End If
                If (clsFc.UpdateCustomerBillTo("9999999999", "9999999999", "IRPC", "", "", conn, tran) = 0) Then
                    If (clsFc.InsertCustomerBillTo("9999999999", "9999999999", "IRPC", "", "", conn, tran) < 1) Then
                        condition = False
                    End If
                End If
                For i As Integer = 0 To ZBABI_BILLTO.Count - 1
                    If (clsFc.UpdateCustomerBillTo(ZBABI_BILLTO.Item(i).Bill_To, ZBABI_BILLTO.Item(i).Sold_To, ZBABI_BILLTO.Item(i).Name_1, ZBABI_BILLTO.Item(i).Name_2, ZBABI_BILLTO.Item(i).Name_3, conn, tran) = 0) Then
                        If (clsFc.InsertCustomerBillTo(ZBABI_BILLTO.Item(i).Bill_To, ZBABI_BILLTO.Item(i).Sold_To, ZBABI_BILLTO.Item(i).Name_1, ZBABI_BILLTO.Item(i).Name_2, ZBABI_BILLTO.Item(i).Name_3, conn, tran) < 1) Then
                            condition = False
                        End If
                    End If
                Next

                If (condition) Then
                    clsDB.CommitTran(tran)
                    ' Response.Write("<SCRIPT>alert('บันทึกข้อมูลสำเร็จ');</SCRIPT>")
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ !!');", True)
                Else
                    clsDB.RollBackTran(tran)
                    ' Response.Write("<SCRIPT>alert('เกิดความผิดพลาดในการบันทึก');</SCRIPT>")
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก !!');", True)
                End If
                conn.Close()
                Application.UnLock()
                ZBABI_BILLTO.Clear()
                ZBABI_INPUT.Clear()
                conn.Dispose()

            Else
                Response.Write("<SCRIPT>alert('" & EX_SUBRC.ToString() & "');</SCRIPT>")
            End If

        Catch ex As Exception
            ' Response.Write("<SCRIPT>alert('" & ex.Message & "');</SCRIPT>")
        End Try
        ClassSAP.SAP_CLOSE(SAP_Data)
        'SAP_Data.Connection.Close()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridCustomer.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridCustomer.PageIndex = 0
            Case "prev"
                If (gridCustomer.PageIndex <> 0) Then
                    gridCustomer.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridCustomer.PageIndex <> gridCustomer.PageCount) Then
                    gridCustomer.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridCustomer.PageIndex = gridCustomer.PageCount
        End Select
        gridCustomer.DataSource = ViewState("gridCustomer")
        gridCustomer.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridCustomer.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridCustomer.PageIndex = ddlPage.SelectedIndex
        gridCustomer.DataSource = ViewState("gridCustomer")
        gridCustomer.DataBind()
    End Sub

    Private Sub gridCustomer_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridCustomer.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridCustomer.PageIndex <> 0) Then
            Index = Index Mod (gridCustomer.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            Dim Address As String = ""
            If Not (gridCustomer.DataKeys(Index).Values("STREET").ToString().Trim() = "") Then
                Address = gridCustomer.DataKeys(Index).Values("STREET").ToString().Trim()
            End If
            If Not (gridCustomer.DataKeys(Index).Values("STR_SUPPL3").ToString().Trim() = "") Then
                Address &= "<br />" & gridCustomer.DataKeys(Index).Values("STR_SUPPL3").ToString().Trim()
            End If
            If Not (gridCustomer.DataKeys(Index).Values("LOCATION").ToString().Trim() = "") Then
                Address &= "<br />" & gridCustomer.DataKeys(Index).Values("LOCATION").ToString().Trim()
            End If
            If Not (gridCustomer.DataKeys(Index).Values("District").ToString().Trim() = "") Then
                Address &= "<br />" & gridCustomer.DataKeys(Index).Values("District").ToString().Trim()
            End If
            If Not (gridCustomer.DataKeys(Index).Values("City").ToString().Trim() = "") Then
                Address &= "<br />" & gridCustomer.DataKeys(Index).Values("City").ToString().Trim()
            End If
            Session("CUSTOMER") = gridCustomer.DataKeys(Index).Values("Customer_no") & "$" & gridCustomer.DataKeys(Index).Values("Name") & "$" & Address & "$" & gridCustomer.DataKeys(Index).Values("Postal_Code").ToString().Trim() & "$" & gridCustomer.DataKeys(Index).Values("Default_Pay").ToString().Trim()
            ModalPopupExtender1.Show()
        End If
    End Sub

    Private Sub gridCustomer_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridCustomer.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridCustomer.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridCustomer.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridCustomer.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridCustomer.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub gridCustomer_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridCustomer.RowEditing

    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        ViewState("gridCustomer") = clsGet.Get_CustomerMaster(txtCustomer.Text.Trim())
        gridCustomer.DataSource = ViewState("gridCustomer")
        gridCustomer.DataBind()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        ViewState("gridCustomer") = clsGet.Get_CustomerMaster("")
        gridCustomer.DataSource = ViewState("gridCustomer")
        gridCustomer.DataBind()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class